
#pragma once

enum FONT
{
	FONT_SANS_12 = 0,   // 12pt sans serif font for general use
	FONT_MONO_10 = 1,   // 10pt monospace sans serif font for code and similar

	NUM_FONTS
};

struct TextTexture
{
	GLuint uGLTexture;
	SDL_Surface* pxSurface;

	TextTexture()
	{
		uGLTexture = INVALID_TEXTURE_ID;
		pxSurface = 0;
	}

	TextTexture( GLuint uTex, SDL_Surface* pxSurf )
	{
		uGLTexture = uTex;
		pxSurface = pxSurf;
	}

	bool IsValid()
	{
		return uGLTexture != INVALID_TEXTURE_ID && pxSurface;
	}

	void Free()
	{
		SDL_FreeSurface( pxSurface );
		glDeleteTextures( 1, &uGLTexture );
	}
};

typedef u_int (*LineColourCallback)( const char* szText );

class TextRender
{
	public:

		static void Initialise();
		static void Shutdown();

		static void SetFont( FONT eFont ) { s_eCurrentFont = eFont; }
		static void SetColour( u_int uCol ) { s_uCurrentCol = uCol; s_pfnCurrentLineColourCallback = 0; }
		static void SetLineColourCallback( LineColourCallback pfnCallback ) { s_pfnCurrentLineColourCallback = pfnCallback; }

		static void Print( const char* szText, const u_int uX, const u_int uY );
		static TextTexture PrintToTexture( const char* szText );

		static void GetRenderSize( const char* szText, u_int& uWidth, u_int& uHeight );
		static void GetRenderPosOfChar( const char* szText, u_int uCharIndex, u_int& uOffsetX, u_int& uOffsetY );
		static u_int GetFontAscent();
		static u_int GetCharAtPos( const char* szText, u_int uX, u_int uY );

	private:

		static const int iMAX_HANDLES  = 8;

		static void PrintLine( const char* szText, const u_int uX, const u_int uY );

		static void LoadFont( int iHandle, char* szTypeface, int iSize );

		static bool FillLineBuffer( const char* szText, int iLine );

		static FONT s_eCurrentFont;
		static u_int s_uCurrentCol;
		
		static LineColourCallback s_pfnCurrentLineColourCallback;

		static TTF_Font* s_apxSDL_TTF_Handles[iMAX_HANDLES];
		static char s_szLineBuffer[ 256 ];
};

//eof